package com.linsir.gen.dto;

import com.google.common.collect.Lists;
import com.linsir.core.binding.annotation.BindEntityList;
import com.linsir.core.util.S;
import com.linsir.gen.entity.SysGenTable;
import com.linsir.gen.entity.SysGenTableColumn;
import com.linsir.gen.vo.SysGenTableVO;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import java.util.Iterator;
import java.util.List;

/**
 * @author ：linsir
 * @date ：Created in 2022/10/8 2:31
 * @description：
 * @modified By：
 * @version:
 */
@Data
public class SysGenTableDTO extends SysGenTable {

    @BindEntityList(entity = SysGenTableColumn.class,condition = "this.id = sys_gen_table_column.sys_gen_table_id")
    private List<SysGenTableColumn> columnList;

    private List<SysGenTable> childList;

    private List<String> pkList;

    private SysGenTable parent;


    public SysGenTableDTO() {

    }

    @Override
    @Length(min=1, max=200)
    public String getName()
    {
        return S.lowerCase(super.getName());
    }

    public String getPk()
    {
        if(super.getPk()=="" || super.getPk() == null) {
            super.setPk("id");
        }
        return super.getPk();
    }


    public String getNameAndComments()
    {
        return getName() + (super.getComments() == null ? "" : new StringBuilder("  :  ").append(super.getComments()).toString());
    }

    public void setPkList(List<String> pkList)
    {
        this.pkList = pkList;
        if(this.pkList.size()>=1){
            super.setPk(this.pkList.get(0));
        }
    }

//    public List<String> getImportList()
//    {
//        List importList = Lists.newArrayList();
//        for(Iterator iterator = getColumnList().iterator(); iterator.hasNext();)
//        {
//            SysGenTableColumn column = (SysGenTableColumn)iterator.next();
//            if((column.getIsNotBaseField().booleanValue() || column.isQuery() && "between".equals(column.getQueryType()) && ("createDate".equals(column.getSimpleJavaField()) || "updateDate".equals(column.getSimpleJavaField()))) && S.indexOf(column.getJavaType(), ".") != -1 && !importList.contains(column.getJavaType())) {
//                importList.add(column.getJavaType());
//            }
//            if(column.getIsNotBaseField().booleanValue())
//            {
//                for(Iterator iterator1 = column.getAnnotationList().iterator(); iterator1.hasNext();)
//                {
//                    String ann = (String)iterator1.next();
//                    if(!importList.contains(S.substringBeforeLast(ann, "("))) {
//                        importList.add(S.substringBeforeLast(ann, "("));
//                    }
//                }
//
//            }
//        }
//
//        if(getChildList() != null && getChildList().size() > 0)
//        {
//            if(!importList.contains("java.util.List")) {
//                importList.add("java.util.List");
//            }
//            if(!importList.contains("com.google.common.collect.Lists")) {
//                importList.add("com.google.common.collect.Lists");
//            }
//        }
//        return importList;
//    }

    public Boolean getParentExists()
    {
        if ((this.parent != null) && (S.isNotBlank(super.getParentTable())) && (S.isNotBlank(super.getParentTableFk()))) {
            return Boolean.valueOf(true);
        }
        return Boolean.valueOf(false);
    }

    public Boolean getCreateDateExists()
    {
        for (SysGenTableColumn c : this.columnList) {
            if ("create_date".equals(c.getName())) {
                return Boolean.valueOf(true);
            }
        }
        return Boolean.valueOf(false);
    }

    public Boolean getUpdateDateExists()
    {
        for (SysGenTableColumn c : this.columnList) {
            if ("update_date".equals(c.getName())) {
                return Boolean.valueOf(true);
            }
        }
        return Boolean.valueOf(false);
    }

    public Boolean getDelFlagExists()
    {
        for (SysGenTableColumn c : this.columnList) {
            if ("del_flag".equals(c.getName())) {
                return Boolean.valueOf(true);
            }
        }
        return Boolean.valueOf(false);
    }
}
